package fileServer;

public class Lock {

	private int readers = 0;
	
	private boolean writing = false;
	
// --------------------------------------------------------------------
	
	public synchronized void getReadersLock(){
		
		while(writing){
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}
		readers++;
	}
	
	public synchronized void releaseReadersLock(){
		
		readers--;
		notifyAll();
	}
	
	public synchronized void getWritersLock(){
		
		while((readers > 0) || writing){
			try {
				wait();
			} catch (InterruptedException e) {
			}
		}
		writing = true;
	}
	
	public synchronized void releaseWritersLock(){
		
		writing = false;
		notifyAll();
	}
}
